package com.zxy.service.Impl;

import com.zxy.dao.BookMapper;
import com.zxy.pojo.Book;
import com.zxy.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.List;

@Service
@Transactional
public class BookServiceImpl implements BookService {

//    static BookMapper initMapper(){
//        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
//        BookMapper bookMapper=applicationContext.getBean(BookMapper.class);
//        return bookMapper;
//    }
//    BookMapper bookMapper=initMapper();
    public List<Book> getAll() {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
        BookMapper bookMapper=applicationContext.getBean(BookMapper.class);
        List<Book> list=bookMapper.getAll();
        return list;
    }

    public Book SelectByExample(Integer BookId) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
        BookMapper bookMapper=applicationContext.getBean(BookMapper.class);
        Book book=bookMapper.SelectByExample(BookId);
        return book;
    }


    /**
     * 在做添加事务时，应该增加事务回滚
     *
     * @param book
     * @return
     */
    public int AddBook(Book book) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
        BookMapper bookMapper=applicationContext.getBean(BookMapper.class);
//        BookMapper bookMapper=initMapper();
        int i=bookMapper.AddBook(book);
        return i;
    }

    public int DeleteByExample(Book book) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
        BookMapper bookMapper=applicationContext.getBean(BookMapper.class);
        int i=bookMapper.DeleteByExample(book);
        return i;
    }

    public int UpdateBook(Book book) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
        BookMapper bookMapper=applicationContext.getBean(BookMapper.class);
        int i=bookMapper.UpdateBook(book);
        return i;
    }
}


